package cn.anc.aonicardv.util;

import android.content.Context;
import android.text.TextUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.anc.aonicardv.R;

/**
 * Created by yangdai on 2017/5/24.
 */
public class DateUtils {


    /** 日期格式：yyyy-MM-dd HH:mm:ss **/
    public static final String DF_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    /** 日期格式：yyyy-MM-dd HH:mm:ss **/
    public static final String DF_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    /** 日期格式：yyyy-MM-dd **/
    public static final String DF_YYYY_MM_DD = "yyyy-MM-dd";
    /** 日期格式：yyyy年 **/
    public static final String DF_YYYY_DOT = "yyyy";
    /** 月份格式：dd **/
    public static final String DF_MM = "MM";
    /** 日期格式：dd **/
    public static final String DF_dd = "dd";
    /** 日期格式：HH:mm:ss **/
    public static final String DF_HH_MM_SS = "HH:mm:ss";

    /**
     * form like : yyyy-MM-dd HH:mm:ss
     */
    public static final int FORMAT_ONE = 0;

    /**
     * form like : yyyy/MM/dd HH:mm:ss
     */
    public static final int FORMAT_TWO = 1;

    /**
     * form like : yyyyMMddHHmmss
     */
    public static final int FORMAT_THREE = 2;


    public static String getDate(Date date, boolean isShowYearMonth,
                                 boolean isShowDay, boolean isShowHourMinute, boolean isShowSecond, int format, boolean is24) {
        String yymmPattern;
        String ddPattern;
        String hhmmPattern;
        String hhmmssPattern;
        if (is24) {
            hhmmPattern = "HH:mm";
            hhmmssPattern = "HH:mm:ss";
        } else {
            hhmmPattern = "hh:mm";
            hhmmssPattern = "hh:mm:ss";
        }
        if (format == FORMAT_ONE) {
            yymmPattern = "yyyy-MM";
            ddPattern = "-dd";
        } else if (format == FORMAT_TWO) {
            yymmPattern = "yyyy/MM";
            ddPattern = "/dd";
        } else {
            yymmPattern = "yyyyMM";
            ddPattern = "dd";
            if (is24) {
                hhmmPattern = "HHmm";
                hhmmssPattern = "HHmmss";
            } else {
                hhmmPattern = "hhmm";
                hhmmssPattern = "hhmmss";
            }
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();// 这里格式就是年月天

        if (isShowYearMonth && isShowDay && isShowHourMinute && isShowSecond) {
            simpleDateFormat.applyPattern(yymmPattern + ddPattern + " " + hhmmssPattern);
        } else if (isShowYearMonth && isShowDay && isShowHourMinute && !isShowSecond) {
            simpleDateFormat.applyPattern(yymmPattern + ddPattern + " " + hhmmPattern);
        } else if (isShowYearMonth && !isShowDay && !isShowHourMinute && !isShowSecond) {
            simpleDateFormat.applyPattern(yymmPattern);
        } else if (!isShowYearMonth && isShowDay && !isShowHourMinute && !isShowSecond) {
            simpleDateFormat.applyPattern(ddPattern);
        } else if (isShowYearMonth && isShowDay && !isShowHourMinute && !isShowSecond) {
            simpleDateFormat.applyPattern(yymmPattern + ddPattern);
        } else if (!isShowYearMonth && !isShowDay && isShowHourMinute && isShowSecond) {
            simpleDateFormat.applyPattern(hhmmssPattern);
        }
        return simpleDateFormat.format(date);

    }

    public static String getDataString(String str) {
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        String mDate =  m.replaceAll("").trim();
        if (StringUtils.isEmptyOrNull(mDate)||mDate.length()<14){
            return null;
        }
        return m.replaceAll("").trim().substring(0, 14);
    }

    public static String getDate(long date, boolean isShowYearMonth,
                                 boolean isShowDay, boolean isShowHourMinute, boolean isShowSecond, int format) {
        String yymmPattern;
        String ddPattern;
        String hhmmPattern = "HH:mm";
        String hhmmssPattern = "HH:mm:ss";
        if (format == FORMAT_ONE) {
            yymmPattern = "yyyy-MM";
            ddPattern = "-dd";
        } else if (format == FORMAT_TWO) {
            yymmPattern = "yyyy/MM";
            ddPattern = "/dd";
        } else {
            yymmPattern = "yyyyMM";
            ddPattern = "dd";
            hhmmPattern = "HHmm";
            hhmmssPattern = "HHmmss";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();// 这里格式就是年月天

        if (isShowYearMonth && isShowDay && isShowHourMinute && isShowSecond) {
            simpleDateFormat.applyPattern(yymmPattern + ddPattern + " " + hhmmssPattern);
        } else if (isShowYearMonth && isShowDay && isShowHourMinute && !isShowSecond) {
            simpleDateFormat.applyPattern(yymmPattern + ddPattern + " " + hhmmPattern);
        } else if (isShowYearMonth && !isShowDay && !isShowHourMinute && !isShowSecond) {
            simpleDateFormat.applyPattern(yymmPattern);
        } else if (!isShowYearMonth && isShowDay && !isShowHourMinute && !isShowSecond) {
            simpleDateFormat.applyPattern(ddPattern);
        } else if (isShowYearMonth && isShowDay && !isShowHourMinute && !isShowSecond) {
            simpleDateFormat.applyPattern(yymmPattern + ddPattern);
        } else if (!isShowYearMonth && !isShowDay && isShowHourMinute && isShowSecond) {
            simpleDateFormat.applyPattern(hhmmssPattern);
        }
        return simpleDateFormat.format(date);
    }

    public static Date getDate(String date, boolean isShowYearMonth,
                               boolean isShowDay, boolean isShowHourMinute, boolean isShowSecond, int format) {
        try {
            if (TextUtils.isEmpty(date))
                return null;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
            String yymmPattern;
            String ddPattern;
            String hhmmPattern = "HH:mm";
            String hhmmssPattern = "HH:mm:ss";
            if (format == FORMAT_ONE) {
                yymmPattern = "yyyy-MM";
                ddPattern = "-dd";
            } else if (format == FORMAT_TWO) {
                yymmPattern = "yyyy/MM";
                ddPattern = "/dd";
            } else {
                yymmPattern = "yyyyMM";
                ddPattern = "dd";
                hhmmPattern = "HHmm";
                hhmmssPattern = "HHmmss";
            }
            if (isShowYearMonth && isShowDay && !isShowHourMinute) {
                simpleDateFormat.applyPattern(yymmPattern + ddPattern);
            } else if (isShowYearMonth && isShowDay && isShowHourMinute && !isShowSecond) {
                simpleDateFormat.applyPattern(yymmPattern + ddPattern + " " + hhmmPattern);
            } else if (isShowYearMonth && isShowDay && isShowHourMinute && isShowSecond) {
                simpleDateFormat.applyPattern(yymmPattern + ddPattern + " " + hhmmssPattern);
            } else if (isShowYearMonth && !isShowDay && !isShowHourMinute && !isShowSecond) {
                simpleDateFormat.applyPattern(yymmPattern);
            } else if (!isShowYearMonth && isShowDay && !isShowHourMinute && !isShowSecond) {
                simpleDateFormat.applyPattern(ddPattern);
            } else if (!isShowYearMonth && !isShowDay && isShowHourMinute && isShowSecond) {
                simpleDateFormat.applyPattern(hhmmssPattern);
            }
            return simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int getDaysByDate(String date) {
        String s[] = date.split("-");
        int month = Integer.valueOf(s[1]);
        int year = Integer.valueOf(s[0]);
        switch (month) {
            case 1:
                return 31;

            case 2:
                if (year % 100 != 0 && year % 4 == 0) {
                    return 29;
                } else {
                    return 28;
                }
            case 3:

                return 31;
            case 4:

                return 30;
            case 5:

                return 31;
            case 6:

                return 30;
            case 7:

                return 31;
            case 8:

                return 31;
            case 9:

                return 30;
            case 10:

                return 31;
            case 11:

                return 30;
            case 12:

                return 31;
            default:
                break;
        }

        return 30;

    }

    public static List<Date> getDatesBetweenTwoDate(String start, String end) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Date beginDate = sdf.parse(start);
            Date endDate = sdf.parse(end);
            List<Date> lDate = new ArrayList<Date>();
            lDate.add(beginDate);// 把开始时间加入集合
            Calendar cal = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            cal.setTime(beginDate);
            boolean bContinue = true;
            while (bContinue) {
                // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
                cal.add(Calendar.MONTH, 1);
                // 测试此日期是否在指定日期之后
                if (endDate.after(cal.getTime())) {
                    lDate.add(cal.getTime());
                } else {
                    break;
                }
            }
            lDate.add(endDate);// 把结束时间加入集合
            return lDate;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }


    public static int compareDateResult(Date statrDate, Date endDate) {
        try {
            Date dt1 = statrDate;
            Date dt2 = endDate;
            if (dt1.getTime() > dt2.getTime()) {

                return -1;
            } else if (dt1.getTime() < dt2.getTime()) {

                return 1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    public static String formatDateToHowLongAgo(Context context, String date) {
        long dstTime = getDate(date, true, true, true, true, FORMAT_ONE).getTime();
        int time = (int) (System.currentTimeMillis() - dstTime);
        if (time > 3600000 && time < 24 * 3600000) {
            return (time / 3600000) + context.getResources().getString(R.string.hour_before);
        } else if (time < 3600000) {
            return (time / 60000) + context.getResources().getString(R.string.min_before);
        }
        return date;
    }

    /**
     * 将日期以yyyy-MM-dd HH:mm:ss格式化
     */
    public static String formatDateTime(Date date, String formater) {
        if (null!=date){
            SimpleDateFormat sdf = new SimpleDateFormat(formater);
            return sdf.format(date);
        }
        return null;

    }

    public static boolean get24HourMode(Context context) {
        return android.text.format.DateFormat.is24HourFormat(context);
    }

    public static String getCurrentDateTime() {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());

    }
    /**
     * 将日期字符串转成日期
     *
     * @param strDate 字符串日期
     * @return java.util.date日期类型
     */
    public static Date parseDate(String strDate, String formater) {
        DateFormat dateFormat = new SimpleDateFormat(formater);
        Date returnDate = null;
        try {
            returnDate = dateFormat.parse(strDate);
        } catch (ParseException e) {

        }
        return returnDate;

    }
    /** 获取当前时间,单位为毫秒 */
    public static long getCurrentDateTimeNills() {
        return getCurrentDate().getTime();
    }

    /**
     * 获取系统当前日期
     *
     * @return
     */
    public static Date getCurrentDate() {
        return new Date();
    }




}
